package com.merry.spring5.aop.xml;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author ybzou
 * 切面类：使用xml实现AOP
 */
@Component
public class LogAspectXml {

    public void beforeMethod(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        System.out.println("xml前置通知... "+":参数... "+args[0]+"。"+args[1]);
    }

    public void afterMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("xml后置通知... "+" : 方法名字"+methodName);
    }

    public void afterThrowMethod(JoinPoint joinPoint,Exception exception){
        System.out.println("xml异常通知...出现异常的方法："+joinPoint.getSignature().getName()+" 异常信息："+exception.getMessage());
    }

    public void afterReturnMethod(JoinPoint joinPoint,Object result){
        System.out.println("xml返回通知...增强的方法是："+joinPoint.getSignature().getName()
        +" 返回结果是："+result);
    }

    public Object aroundMethod(ProceedingJoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        String arg = Arrays.toString(args);
        Object result=null;
        try {
            System.out.println("xml环绕通知...==目标方法之前执行...");
            //调用目标方法
            result = joinPoint.proceed();
            System.out.println("xml环绕通知...==目标方法之后执行...");
        }catch (Throwable throwable){
            System.out.println(throwable.getMessage());
            System.out.println("xml环绕通知...==目标方法出现异常时执行...");
        }finally {
            System.out.println("xml环绕通知...==目标方法执行完毕执行...");
        }
        return result;
    }
}
